package com.sykj.config;


import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yanger
 * @description quartz定时配置类
 * @date 2019/10/16
 */
@Configuration
public class QuartzConfig {

//    @Bean
//    public JobDetail jobDetail1(){
//        return JobBuilder.newJob(QuartzJob1.class).storeDurably().build();
//    }
//
//
//    @Bean
//    public Trigger trigger1(){
//        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                .withIntervalInSeconds(1) //每一秒执行一次
//                .repeatForever(); //永久重复，一直执行下去
//        return TriggerBuilder.newTrigger()
//                .forJob(jobDetail1())
//                .withSchedule(scheduleBuilder)
//                .build();
//    }
//
//    @Bean
//    public JobDetail jobDetail2(){
//        QuartzJobBean quartzJob2 = new QuartzJobBean() {
//            @Override
//            protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                System.out.println("内部类quartzJob2----" + sdf.format(new Date()));
//            }
//        };
//        return JobBuilder.newJob(quartzJob2.getClass()).storeDurably().build();
//    }
//
//    @Bean
//    public Trigger trigger2(){
//        //JobDetail的bean注入不能省略
//        //JobDetail jobDetail3 = JobBuilder.newJob(QuartzJob2.class).storeDurably().build();
//        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                .withIntervalInSeconds(2) //每2秒执行一次
//                .repeatForever(); //永久重复，一直执行下去
//
//        return TriggerBuilder.newTrigger()
//                .forJob(jobDetail2())
//                .withSchedule(scheduleBuilder).build();
//    }


    @Bean
    public JobDetail printTimeJobDetail(){
        return JobBuilder.newJob(QuartzJob1.class)//PrintTimeJob我们的业务类
                .withIdentity("DateTimeJob")//可以给该JobDetail起一个id
                //每个JobDetail内都有一个Map，包含了关联到这个Job的数据，在Job类中可以通过context获取
                .usingJobData("msg", "Hello Quartz")//关联键值对
                .storeDurably()//即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
    }



    @Bean
    public Trigger printTimeJobTrigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/1 * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(printTimeJobDetail())//关联上述的JobDetail
                .withIdentity("quartzTaskService")//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
    }




}